package com.example.fund;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.example.fund.amplitude.Amplitude;
import com.example.fund.amplitude.TimePeriod;
import com.example.fund.candle.CandleImpl;
import com.example.fund.candle.CandleInfo;
import com.example.fund.candle.ICandle;
import com.example.fund.info.FundInfo;
import com.example.fund.util.FundUtil;
import com.example.fund.util.FundUtil1;
import com.example.fund.util.StringUtil;

import android.content.Context;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;  
import android.view.View;  
  
public class DrawView extends View {  
	
	private ParamInfo paramInfo;
	
	private static int bg1 = Color.rgb(233,244,251);
	private static int bg2 = Color.rgb(5, 167, 247);
	private int w;
	private int h;
	private int startIndex;
	private int endIndex;
	private BigDecimal min;
	private BigDecimal max;
	
	private static int[] color = Constant.colorSet;
	private static List<String> dateList = FundUtil.getSHIndexDateList();
	private static FundInfo SHInfo = FundUtil.getFundNetValueFromLocalhost("上证", "0000001");
	private static FundInfo ZZ500Info = FundUtil.getFundNetValueFromLocalhost("中证500", "0000905");
	private FundInfo mixDateFund;
	private List<CandleInfo> candleList = null;

	private List<Double> logList = new ArrayList<Double>();
	
    public DrawView(Context context,AttributeSet set)  
    {  
        super(context,set);
    }
    
    /**
     * 重写该方法,进行绘图  
     */
    @Override  
    protected void onDraw(Canvas canvas)  
    {  
        super.onDraw(canvas);
		//绘图所需的数据参数
		if(paramInfo==null)
		{
			return;
		}

		//初始化数据
		init(canvas);
		//绘制图表
		drawUI(canvas);
    }

	/**
	 * //初始化数据
	 * @param canvas
	 */
	private void init(Canvas canvas)
	{
		//把整张画布绘制成白色
		canvas.drawColor(bg1);

		//计算绘图区的高度、宽度
		h = calHeight();
		w = calWidth();
		startIndex = getStartIndex();
		endIndex = getEndIndex();
		mixDateFund = getMixDateFund();
		BigDecimal[] minAndMax = calMinAndMax();
		min = minAndMax[0];
		max = minAndMax[1];
		calLogList();
	}

	/**
	 * //绘制图表
	 * @param canvas
	 */
	private void drawUI(Canvas canvas)
	{
		Paint paint = new Paint();
		//去锯齿
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL);
		//画横线刻度
		drawHorizontal(canvas,paint);
		//画竖线刻度
		drawVertical(canvas,paint);
		//画走势线
		drawChartsLine(canvas,paint);
		//画折线
		drawSimpleLine(canvas,paint);
		//画上证指数高低点线
		drawHighAndlowLine(canvas,paint);
		//画蜡烛图
		drawCandle(canvas,paint);
		//画年化线
		drawAnnualLine(canvas,paint);

/*		paramInfo.setStartDate("2005-07-18");
		paramInfo.setEndDate("2009-02-27");
		startIndex = getStartIndex();
		endIndex = getEndIndex();
		BigDecimal[] minAndMax = calMinAndMax();
		min = minAndMax[0];
		max = minAndMax[1];
		//画走势线
		drawChartsLine(canvas,paint);
		//画折线
		drawSimpleLine(canvas,paint);

		paramInfo.setStartDate("2008-11-04");
		paramInfo.setEndDate("2013-06-25");
		startIndex = getStartIndex();
		endIndex = getEndIndex();
		minAndMax = calMinAndMax();
		min = minAndMax[0];
		max = minAndMax[1];
		//画走势线
		drawChartsLine(canvas,paint);
		//画折线
		drawSimpleLine(canvas,paint);*/
	}

	/**
	 * 画横线刻度
	 * @param canvas
	 * @param paint
	 */
	private void drawHorizontal(Canvas canvas, Paint paint)
	{
		paint.setColor(color[11]);
		paint.setStrokeWidth(0.5f);
		float d = (float)h/10.00f;
		float td = 0;
		for(int i=0;i<=10;i++)
		{
			paint.setStrokeWidth(0.5f);
			td = i*d;
			if(i==7)
			{
				canvas.drawLine(60, td+((float)h)/20.00f, w, td+((float)h)/20.00f, paint);
				canvas.drawLine(60, td+3*((float)h)/20.00f, w, td+3*((float)h)/20.00f, paint);
				paint.setStrokeWidth(2f);
			}

			canvas.drawLine(0, td, w, td, paint);
		}
		paint.setTextSize(20);

		BigDecimal bigDecimal100 = new BigDecimal("100.00");
		String startDate = paramInfo.getStartDate();
		if(StringUtil.isNullOrEmpty(startDate))
		{
			return;
		}
		String beginValueStr = SHInfo.getActualValues().get(startDate);
		if(StringUtil.isNullOrEmpty(beginValueStr))
		{
			return;
		}
		BigDecimal beginValue = new BigDecimal(beginValueStr);

		td = 0;
		for(int i=0;i<=10;i++)
		{
			td = i*d;
			BigDecimal value = yTransValue((int)td);
			BigDecimal rise = BigDecimal.ZERO;
			if(beginValue!=null && value!=null)
			{
				rise = value.subtract(beginValue).multiply(bigDecimal100).divide(beginValue, 0, RoundingMode.HALF_UP);
			}
			if(i==0)
			{
				canvas.drawText(String.valueOf(max.intValue()), 5, td+25, paint);
				canvas.drawText(rise+"", 5, td+25+20, paint);
			}else if(i==10)
			{
				canvas.drawText(String.valueOf(min.intValue()), 5, td-25, paint);
				canvas.drawText(rise+"", 5, td-5, paint);
			}else if(i==7)
			{
				canvas.drawText(String.valueOf(value.intValue()), 5, td-5, paint);
				canvas.drawText(rise+"", 5, td-2+20, paint);
				float m = td+((float)h)/20.00f;
				BigDecimal mValue = yTransValue((int)m);
				canvas.drawText(String.valueOf(mValue.intValue()), 5, m+7, paint);
				float n = td+3*((float)h)/20.00f;
				BigDecimal nValue = yTransValue((int)n);
				canvas.drawText(String.valueOf(nValue.intValue()), 5, n+7, paint);
			}else
			{
				canvas.drawText(String.valueOf(value.intValue()), 5, td-5, paint);
				canvas.drawText(rise+"", 5, td-2+20, paint);
			}
		}
	}

	/**
	 * 画竖线刻度
	 * @param canvas
	 * @param paint
	 */
	private void drawVertical(Canvas canvas, Paint paint)
	{
		if(paramInfo.isCandle())
		{
			return;
		}
		String startDate = paramInfo.getStartDate();
		String endDate = paramInfo.getEndDate();

		if(ZZ500Info==null||startDate==null || endDate==null || dateList==null || dateList.size()==0 || startIndex<=endIndex)
		{
			return;
		}
		TimePeriod timePeriod = new TimePeriod(new Amplitude());
		List<String> shAmpDateList = timePeriod.getSHAmplitudeDateList(dateList, new BigDecimal("14.0"), new BigDecimal("-14.0"));
		List<String> shAmpDateList2 = timePeriod.getSHAmplitudeDateList(dateList, new BigDecimal("50.0"), new BigDecimal("-40.0"));
		List<String> shAmpDateList1 = new LinkedList<String>();
		List<String> shAmpDateList3 = new LinkedList<String>();
		List<String> shAmpDateList4 = new LinkedList<String>();
		List<String> shAmpDateList5 = new LinkedList<String>();
		for(int i=0;i<shAmpDateList2.size();i++)
		{
			if(i%2==1)
			{
				String dateStr = shAmpDateList2.get(i);
				shAmpDateList3.add(dateStr);
			}else if(i%2==0)
			{
				String dateStr = shAmpDateList2.get(i);
				shAmpDateList4.add(dateStr);
			}
		}

		double laseValue = -1;
		double laseValue1 = -1;
		Map<String, String> actValues = ZZ500Info.getActualValues();
		for(int i=0;i<shAmpDateList.size();i++)
		{
			String dateStr = shAmpDateList.get(i);
			if(shAmpDateList3.contains(dateStr))
			{
				String value = actValues.get(dateStr);
				if(value!=null)
				{
					laseValue = Double.valueOf(value);
				}
			}
			if(laseValue!=-1 && i%2==0)
			{
				String value = actValues.get(dateStr);
				if(value!=null && Double.valueOf(value)<laseValue)
				{
					shAmpDateList1.add(dateStr);
					laseValue = Double.valueOf(value);
				}
			}
			if(shAmpDateList4.contains(dateStr))
			{
				String value = actValues.get(dateStr);
				if(value!=null)
				{
					laseValue1 = Double.valueOf(value);
				}
			}
			if(laseValue1!=-1 && i%2==1)
			{
				String value = actValues.get(dateStr);
				if(value!=null && (Double.valueOf(value)-laseValue1)/laseValue1>0.1)
				{
					shAmpDateList5.add(dateStr);
					laseValue1 = Double.valueOf(value);
				}
			}
		}

		int space = 1;//日期显示间隔:1.按年2.按季3.按月4.按日
		int daySpace = startIndex-endIndex;
		int monthSpace = getMonthSpace();
		if(monthSpace<=120)
		{
			space=2;
			if(monthSpace<=36)
			{
				if(daySpace>60)
				{
					space = 3;
				}else
				{
					space = 4;
				}
			}
		}
		paint.setTextSize(18);
		paint.setStrokeWidth(0.5f);
		if(space==1)
		{
			String lastYear = "";
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				if(date.compareTo("1996-01-22")>0)
				{
					if(shAmpDateList1.contains(date) || shAmpDateList5.contains(date))
					{
						if(shAmpDateList1.contains(date))
						{
							paint.setColor(color[1]);
						}else
						{
							paint.setColor(color[6]);
						}
						if(shAmpDateList2.contains(date))
                        {
							if (shAmpDateList1.contains(date))
							{
								paint.setStrokeWidth(2.5f);
							}else
							{
								paint.setStrokeWidth(2.3f);
							}
                        }else
                        {
                            paint.setStrokeWidth(0.5f);
                        }
						String year = date.substring(2, 4);
						int x = indexToX(i);
						canvas.drawLine(x, 0, x, h, paint);

						String month = date.substring(5, 7);
						String day = date.substring(8, 10);
						if(i==startIndex)
						{
							x = x+15;
						}
						paint.setColor(color[11]);
						if(x-lastX<20)
						{
							continue;
						}
						canvas.drawText(year, x-15, h+25, paint);
						canvas.drawText(month, x-15, h+45, paint);
						canvas.drawText(day, x-15, h+65, paint);
						lastX = x;
					}
				}else
				{
					paint.setColor(color[11]);
					String year = date.substring(2, 4);
					if(!lastYear.equals(year))
					{
						int x = indexToX(i);
						if(x>=100)
						{
							canvas.drawLine(x, 0, x, h, paint);
						}
						String month = date.substring(5, 7);
						String day = date.substring(8, 10);
						if(i==startIndex)
						{
							x = x+15;
						}
						if(x-lastX<20)
						{
							continue;
						}
						canvas.drawText(year, x-15, h+25, paint);
						canvas.drawText(month, x-15, h+45, paint);
						canvas.drawText(day, x-15, h+65, paint);
						lastYear = year;
						lastX = x;
					}
				}
			}
		}else if(space==2)
		{
			String lastSeason = "";
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				if(date.compareTo("1996-01-22")>0)
				{
					if(shAmpDateList1.contains(date) || shAmpDateList5.contains(date))
					{
						if(shAmpDateList1.contains(date))
						{
							paint.setColor(color[1]);
						}else
						{
							paint.setColor(color[6]);
						}
                        if(shAmpDateList2.contains(date))
                        {
							if (shAmpDateList1.contains(date))
							{
								paint.setStrokeWidth(2.5f);
							}else
							{
								paint.setStrokeWidth(2.3f);
							}
                        }else
                        {
                            paint.setStrokeWidth(0.5f);
                        }
						String month = date.substring(5, 7);
						int x = indexToX(i);
						canvas.drawLine(x, 0, x, h, paint);

						String year = date.substring(2, 4);
						String day = date.substring(8, 10);
						if(i==startIndex)
						{
							x = x+15;
						}
						if(x-lastX<20)
						{
							continue;
						}
						paint.setColor(color[11]);
						canvas.drawText(year, x-15, h+25, paint);
						canvas.drawText(month, x-15, h+45, paint);
						canvas.drawText(day, x-15, h+65, paint);
						lastX = x;
					}
				}else
				{
					paint.setColor(color[11]);
					String month = date.substring(5, 7);
					if("01".equals(month) || "04".equals(month) || "07".equals(month) || "10".equals(month))
					{
						if(!lastSeason.equals(month))
						{
							int x = indexToX(i);
							if(x>=100)
							{
								canvas.drawLine(x, 0, x, h, paint);
							}
							String year = date.substring(2, 4);
							String day = date.substring(8, 10);
							if(i==startIndex)
							{
								x = x+15;
							}
							if(x-lastX<20)
							{
								continue;
							}
							canvas.drawText(year, x-15, h+25, paint);
							canvas.drawText(month, x-15, h+45, paint);
							canvas.drawText(day, x-15, h+65, paint);
							lastSeason = month;
							lastX = x;
						}
					}
				}
			}
		}else if(space==3)
		{
			paint.setColor(color[11]);
			String lastMonth = "";
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				String month = date.substring(5, 7);
				if(!lastMonth.equals(month))
				{
					int x = indexToX(i);
					if(x>=100)
					{
						canvas.drawLine(x, 0, x, h, paint);
					}
					String year = date.substring(2, 4);
					String day = date.substring(8, 10);
					if(i==startIndex)
					{
						x = x+15;
					}
					if(x-lastX<20)
					{
						continue;
					}
					canvas.drawText(year, x-15, h+25, paint);
					canvas.drawText(month, x-15, h+45, paint);
					canvas.drawText(day, x-15, h+65, paint);
					lastMonth = month;
					lastX = x;
				}
			}
		}else
		{
			paint.setColor(color[11]);
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				int x = indexToX(i);
				if(x>=100)
				{
					canvas.drawLine(x, 0, x, h, paint);
				}
				String year = date.substring(2, 4);
				String month = date.substring(5, 7);
				String day = date.substring(8, 10);
				if(i==startIndex)
				{
					x = x+15;
				}
				if(x-lastX<20)
				{
					continue;
				}
				canvas.drawText(year, x-15, h+25, paint);
				canvas.drawText(month, x-15, h+50, paint);
				canvas.drawText(day, x-15, h+75, paint);
				lastX = x;
			}
		}
	}

	/**
	 * 画走势线
	 * @param canvas
	 * @param paint
	 */
	private void drawChartsLine(Canvas canvas, Paint paint)
	{
		if(paramInfo.isCandle())
		{
			return;
		}

		boolean isChartsLine = paramInfo.isChartsLine();
		if(!isChartsLine)
		{
			return;
		}
		paint.setStrokeWidth(3);
		//计算基金走势线点集合
		Map<String,List<Point>> fundPointsMap = new LinkedHashMap<String,List<Point>>();

		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		if(selectFundInfoList!=null && selectFundInfoList.size()>0 && dateList!=null)
		{
			int size = selectFundInfoList.size();
			for(int i=0;i<size;i++)
			{
				FundInfo info = selectFundInfoList.get(i);
				List<Point> fundPoints = getFundPoints(info);//把基金净值转化为点集合
				fundPointsMap.put(info.getNumber(),fundPoints);
			}
		}

		//基金交易点坐标
		List<Point> points1 = new ArrayList<Point>();//买入
		List<Point> points2 = new ArrayList<Point>();//卖出

		int i = 0;
		for (String number : fundPointsMap.keySet())
		{
			List<Point> fundPoints = fundPointsMap.get(number);
			if (fundPoints.size()==0)
			{
				continue;
			}
		//	paint.setColor(getIndexColor(number));
			paint.setColor(Constant.colorSet1[Math.min(i++,Constant.colorSet1.length-1)]);
			Point lastP = fundPoints.get(0);
			for (Point p : fundPoints)
			{
				canvas.drawLine(lastP.getX(), lastP.getY(), p.getX(), p.getY(), paint);
				lastP = p;

				if (p.getType()==1)
				{
					points1.add(p);
				}else if(p.getType()==2)
				{
					points2.add(p);
				}
			}
		}

		paint.setTextSize(30);
		paint.setColor(color[6]);
		for (Point p : points1)
		{
			canvas.drawText("●", p.getX()-7, p.getY()+7, paint);
		}
		paint.setColor(color[3]);
		for (Point p : points2) {
			canvas.drawText("●", p.getX()-7, p.getY()+7, paint);
		}
		paint.setTextSize(18);
	}

	/**
	 * 画年化线
	 * @param canvas
	 * @param paint
	 */
	private void drawAnnualLine(Canvas canvas, Paint paint)
	{
		double annual = paramInfo.getAnnual();
		if (annual==0)
		{
			return;
		}

		int i = getMonthSpace();
		if(i<14)
		{
			return;
		}

		double year = new BigDecimal(((double) i)/12.00d).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();;
		BigDecimal bigDecimal100 = new BigDecimal("100.00");
		String startDate = paramInfo.getStartDate();
		if(StringUtil.isNullOrEmpty(startDate))
		{
			return;
		}
		String beginValueStr = SHInfo.getActualValues().get(startDate);
		double beginValue = Double.valueOf(beginValueStr);
		annual = 1+ annual/100.00d;
		double endValue = beginValue*annual;
		double value3 = beginValue*Math.pow(annual,year);
		int y1 = valueToY(new BigDecimal(beginValue));
		int y2 = valueToY(new BigDecimal(endValue));
		int x2 = indexToX(Math.max(0,startIndex-240));
		int y3 = valueToY(new BigDecimal(value3));
		paint.setColor(Color.MAGENTA);
		double x3 = ((double)(x2*y1))/(double) (y1-y2);
		if (x3>w)
		{
			canvas.drawLine(0, y1, w, y3, paint);
		}else
		{
			canvas.drawLine(0, y1, (int)x3, 0, paint);
		}



	}

	/**
	 * 把基金净值转化为点集合
	 * @param info
	 * @return
	 */
	private List<Point> getFundPoints(FundInfo info)
	{
		List<Point> fundPoints = new LinkedList<Point>();

		double year = ((double) (startIndex-endIndex))/240.00d;
		double pRate = 1*year;
		double nRate = -1*year;
		List<String> amplitudeDateList = new Amplitude().getAmplitudeDateList(info, dateList, pRate, nRate);
		String number = info.getNumber();
		if ("0000000".equals(info.getNumber()))
		{
			number = FundUtil.getFundNumber(info.getName());
		}
		String account = paramInfo.getAccount();
		Set<String> tradeSet1 = FundUtil1.getTradeSet(number, account,1);
		Set<String> tradeSet2 = FundUtil1.getTradeSet(number, account,2);

		int lastX = -1;
		int lastY = -1;
		String sdateStr = dateList.get(startIndex);
		BigDecimal beginV = new BigDecimal(SHInfo.getActualValues().get(sdateStr));
		BigDecimal beginValue0 = beginV;
		BigDecimal beginValue = null;//起始实际净值
		for(int j=startIndex;j>=endIndex;j--)
		{
			String dateStr = dateList.get(j);
			String actValueStr = info.getActualValues().get(dateStr);
			if(actValueStr!=null)
			{
				BigDecimal actValue = new BigDecimal(actValueStr);
				if(beginValue==null)
				{
				//	String beginValueStr = SHInfo.getActualValues().get(dateStr);
				//	beginValue0 = new BigDecimal(beginValueStr);
                    String beginV0 = mixDateFund.getActualValues().get(sdateStr);
                    String beginV1 = mixDateFund.getActualValues().get(dateStr);
                    if (beginV0!=null && beginV1!=null)
                    {
                        beginValue0 = beginValue0.multiply(new BigDecimal(beginV1)).divide(new BigDecimal(beginV0), 2,RoundingMode.HALF_UP);
                    }
					beginValue = actValue;
				}
				BigDecimal value = beginValue0.multiply(actValue).divide(beginValue, 2,RoundingMode.HALF_UP);

				int x = indexToX(j);
				int y = valueToY(value);

				if(amplitudeDateList.contains(dateStr) || x-lastX>5
						||tradeSet1.contains(dateStr)||tradeSet2.contains(dateStr))
				{

					if(lastX==-1)
					{
						lastX = x;
					}
					if(lastY==-1)
					{
						lastY = y;
					}
					if(tradeSet1.contains(dateStr))
					{
						fundPoints.add(new Point(x,y,1));
					}else if(tradeSet2.contains(dateStr))
					{
						fundPoints.add(new Point(x,y,2));
					}else {
						fundPoints.add(new Point(x,y));
					}

					lastX = x;
					lastY = y;
				}
			}
		}

		return fundPoints;
	}

	/**
	 * 画折线
	 * @param canvas
	 * @param paint
	 */
	private void drawSimpleLine(Canvas canvas, Paint paint)
	{
		if(paramInfo.isCandle())
		{
			return;
		}
		boolean isChartsLine = paramInfo.isChartsLine();
		if(!isChartsLine)
		{
			return;
		}
		boolean isSimpleLine = paramInfo.isSimpleLine();
		if(!isSimpleLine)
		{
			return;
		}
		String positiveRate = paramInfo.getPositiveRate();
		String negativeRate = paramInfo.getNegativeRate();
		if(positiveRate==null || positiveRate.trim().length()==0 || negativeRate==null || negativeRate.trim().length()==0)
		{
			return;
		}
		paint.setStrokeWidth(3);
		//计算基金波动涨幅值
		double pRate = Double.valueOf(positiveRate);
		double nRate = Double.valueOf(negativeRate);
		Map<String,List<Point>> fundSimplePointsMap = new LinkedHashMap<String,List<Point>>();
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		if(selectFundInfoList!=null && selectFundInfoList.size()>0 && dateList!=null)
		{
			List<String> amplitudeDateList = new Amplitude().getAmplitudeDateList(selectFundInfoList.get(0),dateList,pRate,nRate);
			int size = selectFundInfoList.size();
			for(int i=0;i<size;i++)
			{
				FundInfo info = selectFundInfoList.get(i);

				List<Point> fundSimplePoints = getFundSimpleLinePoints(info,amplitudeDateList);//把基金波动涨幅值转化为点集合
				fundSimplePointsMap.put(info.getNumber(),fundSimplePoints);
			}
		}

		//画拆线
		int i = 0;
		for (String number : fundSimplePointsMap.keySet())
		{
			List<Point> fundSimplePoints = fundSimplePointsMap.get(number);
			if (fundSimplePoints.size()==0)
			{
				continue;
			}
		//	paint.setColor(getIndexColor(number));
			paint.setColor(Constant.colorSet1[Math.min(i++,Constant.colorSet1.length-1)]);
			Point lastP = fundSimplePoints.get(0);
			for (Point p : fundSimplePoints)
			{
				canvas.drawLine(lastP.getX(), lastP.getY(), p.getX(), p.getY(), paint);
				lastP = p;
			}
		}

		//绘涨幅值
		for (String number : fundSimplePointsMap.keySet())
		{
			List<Point> fundSimplePoints = fundSimplePointsMap.get(number);
			if (fundSimplePoints.size()==0)
			{
				continue;
			}

			for (Point p : fundSimplePoints)
			{
				int x = p.getX();
				int y = p.getY();
				String rise = p.getAmplitudeRise();

				if(w-x<30)
				{
					x = x-60;
				}else
				{
					x = x-17;
				}
				if(y<30)
				{
					y = y+30;
				}
				if(x>10)
				{
					paint.setStrokeWidth(1);
					if(rise.contains("-"))
					{
						paint.setColor(color[3]);
						y = y+20;
						canvas.drawText(rise, x, y, paint);
					}else
					{
						paint.setColor(color[6]);
						canvas.drawText(rise, x, y, paint);
					}
				}
				paint.setStrokeWidth(3);
			}
		}
	}

    /**
     * 把基金波动涨幅值转化为点集合
     * @param info
     * @param
     * @param
     * @return
     */
	private List<Point> getFundSimpleLinePoints(FundInfo info,List<String> amplitudeDateList)
	{
		List<Point> simplePoints = new LinkedList<Point>();

		Map<String,String> amplitudeMap = new Amplitude().getAmplitudeDateMap(info, amplitudeDateList);
		int lastX = -1;
		int lastY = -1;
		String sdateStr = dateList.get(startIndex);
		BigDecimal beginV = new BigDecimal(SHInfo.getActualValues().get(sdateStr));
		BigDecimal beginValue0 = beginV;
		BigDecimal beginValue = null;//起始实际净值
		for(int j=startIndex;j>=endIndex;j--)
		{
			String dateStr = dateList.get(j);
			String actValueStr = info.getActualValues().get(dateStr);
			if(actValueStr!=null)
			{
				BigDecimal actValue = new BigDecimal(actValueStr);
				if(beginValue==null)
				{
				//	String beginValueStr = SHInfo.getActualValues().get(dateStr);
				//	beginValue0 = new BigDecimal(beginValueStr);
					String beginV0 = mixDateFund.getActualValues().get(sdateStr);
					String beginV1 = mixDateFund.getActualValues().get(dateStr);
					if (beginV0!=null && beginV1!=null)
					{
						beginValue0 = beginValue0.multiply(new BigDecimal(beginV1)).divide(new BigDecimal(beginV0), 2,RoundingMode.HALF_UP);
					}
					beginValue = actValue;
				}
				if(amplitudeMap.keySet().contains(dateStr))
				{
					BigDecimal value = beginValue0.multiply(actValue).divide(beginValue, 2,RoundingMode.HALF_UP);
					String rise = amplitudeMap.get(dateStr);

					int x = indexToX(j);
					int y = valueToY(value);

					simplePoints.add(new Point(x,y,rise));
				}
			}
		}

		return simplePoints;
	}



    /**
     * 画上证指数高低点线
     * @param canvas
     * @param paint
     */
	private void drawHighAndlowLine(Canvas canvas, Paint paint) 
	{
		if(paramInfo.isCandle())
		{
			return;
		}
		BigDecimal pRate = new BigDecimal("50.00");
		BigDecimal nRate = new BigDecimal("-40.00");
		if(dateList==null || dateList.size()==0)
		{
			return;
		}
		Map<String, String> SHInfoV = ZZ500Info.getActualValues();
		List<String> amplitudeDateList = new TimePeriod(new Amplitude()).getSHAmplitudeDateList(dateList, pRate, nRate);
		paint.setColor(color[13]);
		paint.setStrokeWidth(1.5f); 
		int lastX = -1;
		String lastDate = null;
		float y = 7*((float)h/10.00f);
		for(int i=startIndex;i>endIndex;i--)
		{
			String date = dateList.get(i);
			if(amplitudeDateList.contains(date))
			{
				int x = indexToX(i);
				if(lastX==-1)
				{
					lastX = x;
					lastDate = date;
				}
				String lastValue = SHInfoV.get(lastDate);
				String value = SHInfoV.get(date);
				if(lastValue!=null && value!=null && !lastValue.equals(value))
				{
					if(Double.valueOf(value)>Double.valueOf(lastValue))
					{
						paint.setColor(color[6]);
					}else
					{
						paint.setColor(color[3]);
					}
					canvas.drawLine(lastX, y, x, y, paint);
					lastX = x;
					lastDate = date;
				}
			}
		}
	}
	
    /**
     * 画十字线、画长按时的竖线
     * @param canvas
     * @param paint
     */
	private void drawTenLine(Canvas canvas, Paint paint) 
	{
//		paint.setColor(color[8]);  
//        paint.setStrokeWidth(2); 
//        float x = paramInfo.getX();
 //       float y = paramInfo.getY();
//		canvas.drawLine(x, 0, x, h, paint);
//		canvas.drawLine(0, y, w, y, paint);
		//画长按时的竖线
/*		List<Float> coordinateXList = paramInfo.getCoordinateXList();
		if(coordinateXList!=null)
		{
			for(int i=0;i<coordinateXList.size();i++)
			{
				float ax = coordinateXList.get(i);
				canvas.drawLine(ax, 0, ax, h, paint);
			}
			
		}*/
	}
	
	/**
	 * 画蜡烛图
	 * @param canvas
	 * @param paint
	 */
	private void drawCandle(Canvas canvas, Paint paint)
	{
		if(!paramInfo.isCandle())
		{
			return;
		}
		
		if(candleList==null)
		{
			ICandle iCandle = new CandleImpl();
			candleList = iCandle.getCandleInfoList();
		}
		if(candleList==null || candleList.size()==0)
		{
			return;
		}
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		String selectNumber = "";
		if(selectFundInfoList!=null && selectFundInfoList.size()>0)
		{
			selectNumber = selectFundInfoList.get(selectFundInfoList.size()-1).getNumber();
		}
		float x = 50;
		for(int i=0,s=candleList.size();i<s;i++)
		{
			CandleInfo candle = candleList.get(i);
			double closePosition = candle.getClosePosition();
			
		    paint.setStyle(Style.FILL);//实心矩形框 
		    paint.setColor(color[6]);
		    if(selectNumber!=null && selectNumber.equals(candle.getNumber()))
		    {
		    	paint.setColor(color[1]);
		    }
		    float y1 = (float)(1.00d-closePosition)*h-4;
		    canvas.drawRect(new RectF(x, y1, x+10, (float)h-4), paint);
/*		    if(y1<200)
		    {
		    	y1 = 100.00f;
		    }*/
		    paint.setColor(color[11]);
		//    paint.setStrokeWidth(4.5f);
		    String name = candle.getName();
		    paint.setTextSize(18);
		    String cp = new BigDecimal(closePosition*100).setScale(0, RoundingMode.HALF_UP).toString();
		    if(cp.length()==1)
		    {
		    	drawText(canvas,cp, x, y1-5, paint,0);
		    }else
		    {
		    	drawText(canvas,cp, x-4, y1-5, paint,0);
		    }
		    paint.setTextSize(25);
		    for(int j=name.length()-1;j>=0;j--)
		    {
		    	drawText(canvas,name.charAt(j)+"",x-7, y1-=25,paint,0);
		    }
		    
		    x = x+24;
		}
	}

    /**
     * 重绘drawView界面,提供给外部调用重绘界面
     */
    public void rePaintDrawView(ParamInfo paramInfo)
    {
        this.paramInfo = paramInfo;
        this.invalidate();
    }
	
	/**
	 * 计算绘图区的宽度
	 * @return
	 */
    private int calWidth() {
    	int w = this.getWidth()-7;
   // 	w = w-25*3-w%10;
		return w;
	}
	
	/**
	 * 计算绘图区的高度
	 * @return
	 */
	private int calHeight()
	{
		int h = this.getHeight();
//		h = h-30*3-h%10;
		return h;
	}

	
	/**
	 * 计算两个交易日期之间的交易日期数
	 * @return
	 */
	private int getMonthSpace()
	{
		String startDate = paramInfo.getStartDate();
		String endDate = paramInfo.getEndDate();
		int n =0;
		Set<String> monthSet = new HashSet<String>();
		if(startDate!=null && endDate!=null && dateList!=null && dateList.size()>0)
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar bef = Calendar.getInstance();
			Calendar aft = Calendar.getInstance();
			try {
				bef.setTime(sdf.parse(startDate));
				aft.setTime(sdf.parse(endDate));
			}catch (Exception e)
			{
				e.printStackTrace();
			}

			int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
			int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
			n = Math.abs(month + result);
		}
		return n;
	}
	
	/**
	 * 获取开始日期下标
	 * @return
	 */
	private int getStartIndex()
	{
		String startDate = paramInfo.getStartDate();
		int n = 0;
		if(!StringUtil.isNullOrEmpty(startDate) && dateList!=null)
		{
			int i = dateList.indexOf(startDate);
			if(i>0)
			{
				n = i;
			}
		}
		return n;
	}
	
	/**
	 * 获取结束日期下标
	 * @return
	 */
	private int getEndIndex()
	{
		String endDate = paramInfo.getEndDate();
		int n = 0;
		if(!StringUtil.isNullOrEmpty(endDate) && dateList!=null)
		{
			int i = dateList.indexOf(endDate);
			if(i>0)
			{
				n = i;
			}
		}
		return n;
	}
	
	/**
	 * 日期下标转X坐标
	 * @return
	 */
	public int indexToX(int i)
	{
		return startIndex>endIndex?((startIndex-i)*w)/(startIndex-endIndex):0;
	}
	
	/**
	 * X坐标转日期下标
	 * @return
	 */
	public int xToIndex(int x)
	{
		return w>0?startIndex-(x*(startIndex-endIndex)/w):0;
	}

	/**
	 * 涨幅值转Y坐标 
	 * @param
	 * @return
	 */
	private int valueToY(BigDecimal actV)
	{
		if(max.compareTo(min)==0)
		{
			return 0;
		}
		int y = 0;
		if (paramInfo.isLogY())
		{
			double v1 = actV.doubleValue();
			int i=0;
			for (int s=logList.size()-1;i<=s;i++)
			{
				double v = logList.get(i);
				if (v1<=v || i==s)
				{
					break;
				}
			}
			y = (int) ((double)h-((double)(i*h))/((double) (logList.size()-1)));
		}else
		{
			y = (max.subtract(actV).multiply(new BigDecimal(h)).divide(max.subtract(min),2,RoundingMode.HALF_UP)).intValue();
		}

		return y;
	}
	
	/**
	 * 坐标Y值转换成对应涨幅
	 * @param
	 */
	private BigDecimal yTransValue(int y)
	{
		BigDecimal rise = BigDecimal.ONE;
		if (paramInfo.isLogY())
		{
			int i = (int) ((h - y)*((double) (logList.size()-1))/((double)h));
			if (i<logList.size())
			{
				rise = new BigDecimal (logList.get(i));
			}
		}else
		{
			rise = max.subtract(max.subtract(min).multiply(new BigDecimal(y)).divide(new BigDecimal(h),2,RoundingMode.HALF_UP));
		}
		return rise;
	}

	/**
	 * 查询除了参考基金对象外其他最早时间的基金
	 * @return
	 */
	private FundInfo getMixDateFund()
	{
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();


		if(selectFundInfoList==null || selectFundInfoList.size()==0
			|| dateList==null || dateList.size()==0)
		{
			return new FundInfo();
		}
		FundInfo info = null;
		for(int j=startIndex;j>=endIndex;j--)
		{
			String dateStr = dateList.get(j);
			if (StringUtil.isNullOrEmpty(dateStr))
			{
				continue;
			}
			for(int i=0;i<selectFundInfoList.size();i++)
			{
				FundInfo info1 = selectFundInfoList.get(i);
				if (info1.getActualValues().get(dateStr)!=null)
				{
					info = info1;
					break;
				}
			}
			if (info!=null)
			{
				break;
			}
		}

		if(info==null)
		{
			info = selectFundInfoList.get(0);
		}

		return info;
	}

	/**
	 * 计算最小最大值
	 * @return
	 */
	private BigDecimal[] calMinAndMax()
	{
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		BigDecimal min = BigDecimal.ZERO;//找出最小值
		BigDecimal max = BigDecimal.ZERO;//找出最大值
		String startDate = paramInfo.getStartDate();
		String beginValueStr = SHInfo.getActualValues().get(startDate);
		if(selectFundInfoList!=null && dateList!=null && !StringUtil.isNullOrEmpty(beginValueStr))
		{
			BigDecimal beginV = new BigDecimal(beginValueStr);
			BigDecimal beginValue0 = beginV;
			min = beginValue0;
			max = beginValue0;
			for(int i=0;i<selectFundInfoList.size();i++)
			{
				FundInfo info = selectFundInfoList.get(i);
				Map<String, String> actValues = info.getActualValues();//实际净值
				BigDecimal beginValue = null;//起始实际净值
				for(int j=startIndex;j>=endIndex;j--)
				{
					String dateStr = dateList.get(j);
					String actValueStr = actValues.get(dateStr);
					if(actValueStr!=null)
					{
						BigDecimal actValue = new BigDecimal(actValueStr);
						if(beginValue==null)
						{
					//		beginValueStr = SHInfo.getActualValues().get(dateStr);
					//		beginValue0 = new BigDecimal(beginValueStr);
                            String beginV0 = mixDateFund.getActualValues().get(startDate);
                            String beginV1 = mixDateFund.getActualValues().get(dateStr);
                            if (beginV0!=null && beginV1!=null)
                            {
                                beginValue0 = beginValue0.multiply(new BigDecimal(beginV1)).divide(new BigDecimal(beginV0), 2,RoundingMode.HALF_UP);
                            }
							beginValue = actValue;
						}
						BigDecimal rise = beginValue0.multiply(actValue).divide(beginValue, 2,RoundingMode.HALF_UP);
						if(min.compareTo(rise)==1)
						{
							min = rise;
						}
						if(max.compareTo(rise)==-1)
						{
							max = rise;
						}
					}
				}
				beginValue0 = beginV;
			}
		}

		return new BigDecimal[]{min,max};
	}

	private void calLogList()
	{
		logList.clear();
		double dmin = min.doubleValue();
        double dmax = max.doubleValue();
		for (int i=0;i<=10000;i++)
		{
			double v = dmin*Math.pow((1.002d),i);
			logList.add(v);
			if (v>dmax)
            {
                break;
            }
		}
	}
	
	private int getIndexColor(String number)
	{
		Integer c = FundUtil1.getIndexColor(number);

		if(c==null)
		{
			c = color[0];
		}
		return c;
	}
	
	public int getW() {
		return w;
	}
	public int getH() {
		return h;
	}

    void drawText(Canvas canvas ,String text , float x ,float y,Paint paint ,float angle){  
        if(angle != 0){  
            canvas.rotate(angle, x, y);   
        }  
        canvas.drawText(text, x, y, paint);  
        if(angle != 0){  
            canvas.rotate(-angle, x, y);   
        }  
    }
}  
